package com.woniuxy.jiapei.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.jiapei.exception.MyException;
import com.woniuxy.jiapei.pojo.HosDept;
import com.woniuxy.jiapei.pojo.Hospital;
import com.woniuxy.jiapei.pojo.HospitalDept;
import com.woniuxy.jiapei.service.HosDeptService;
import com.woniuxy.jiapei.service.HospitalDeptService;
import com.woniuxy.jiapei.service.HospitalService;
import com.woniuxy.jiapei.util.LvFeiLongDateUtils;
import com.woniuxy.jiapei.util.R;
import com.woniuxy.jiapei.util.ResultCode;
import com.woniuxy.jiapei.util.ResultMsg;
import com.woniuxy.jiapei.vo.HospitalDeptQuery;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lr
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/jiapei/hos-dept")
@CrossOrigin
public class HosDeptController {
    @Autowired
    private HospitalDeptService hospitalDeptService;
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HosDeptService hosDeptService;

    @GetMapping("/hospitalDepts")
    @ApiOperation("分页查询所有科室")
    public R getHospitalDeptList(@ApiParam("当前页") @RequestParam(value = "page", required = true, defaultValue = "1") Integer page, @ApiParam("分页标准") @RequestParam(value = "limit", required = true, defaultValue = "2") Integer limit, HospitalDeptQuery hospitalDeptQuery) throws ParseException, MyException {

        Date[] dates = LvFeiLongDateUtils.parseDateStrType(hospitalDeptQuery.getStartTime(), hospitalDeptQuery.getEndTime());


        QueryWrapper hosDeptQueryWrapper = new QueryWrapper();

        hosDeptQueryWrapper.eq("hos_id", hospitalDeptQuery.getHospitalId());

        List<HosDept> hosDepts = hosDeptService.list(hosDeptQueryWrapper);
        List<Integer> deptIds = new ArrayList<>();

        for (HosDept hosDept : hosDepts) {
            deptIds.add(hosDept.getDeptId());
        }

        Page pageBean = new Page(page, limit);


        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(hospitalDeptQuery.getDeptname())) {
            queryWrapper.like("deptname", hospitalDeptQuery.getDeptname());
        }
        if (!StringUtils.isEmpty(hospitalDeptQuery.getStartTime())) {
            queryWrapper.ge("deptcreatetime", dates[0]);
        }
        if (!StringUtils.isEmpty(hospitalDeptQuery.getEndTime())) {
            queryWrapper.le("deptcreatetime", dates[1]);
        }
        if (deptIds != null && deptIds.size() > 0) {
            queryWrapper.in("id", deptIds);
        }
        Page resultBean = hospitalDeptService.page(pageBean, queryWrapper);
        List<HospitalDept> records = resultBean.getRecords();

        long total = resultBean.getTotal();

        List<Hospital> hospitals = hospitalService.list();

        if (records != null && records.size() > 0) {
            return R.ok().data("records", records).data("total", total).data("hospitals", hospitals);
        } else {
            throw new MyException(ResultCode.GETDEPTBYPAGEFAILED, ResultMsg.GETDEPTBYPAGEFAILEDMSG);
        }
    }

    @GetMapping("/hospitalDept/{id}")
    @ApiOperation("根据id查询科室信息")
    public R getHospitalDeptById(@PathVariable Integer id) throws MyException {
        HospitalDept hospitalDept = hospitalDeptService.getById(id);
        if (hospitalDept != null) {
            return R.ok().data("hospitalDept", hospitalDept);
        } else {
            throw new MyException(ResultCode.GETDEPYBYIDFAILED, ResultMsg.GETDEPTBYIDFAILEDMSG);
        }
    }

    @PutMapping("/hospitalDepts")
    @ApiOperation("修改科室信息")
    public R updateHospitalDept(@RequestBody HospitalDept hospitalDept) throws MyException {
        boolean res = hospitalDeptService.updateById(hospitalDept);
        if (res) {
            return R.ok().message("修改科室信息成功");
        } else {
            throw new MyException(ResultCode.UPDATEHOSPITALDEPTFAILED, ResultMsg.UPDATEHOSPITALDEPTFAILEDMSG);
        }
    }

    @DeleteMapping("/hospitalDept/{id}")
    @ApiOperation("根据id删除科室信息")
    public R deleteHospitalDeptById(@PathVariable Integer id) throws MyException {
        boolean res = hospitalDeptService.removeById(id);
        if (res) {
            return R.ok().message("删除科室信息成功");
        } else {
            throw new MyException(ResultCode.DELETEHOSPITALDEPTFAILED, ResultMsg.DELETEHOSPITALDEPTFAILEDMSG);
        }

    }

    @PostMapping("/hospitalDepts")
    @ApiOperation("新增科室信息")
    public R addHospitalDept(@RequestBody HospitalDept hospitalDept) throws MyException {
        boolean save = hospitalDeptService.save(hospitalDept);
        if (save) {
            return R.ok().message("新增科室信息成功");
        } else {
            throw new MyException(ResultCode.ADDHOSPITALDEPTFAILED, ResultMsg.ADDHOSPITALDEPTFAILEDMSG);
        }
    }

}

